/**
 * project: finance-manage
 * package: com.example.services
 * 服务处理 > 数据处理
 */

package com.example.services;

import com.example.controllers.DataControl;
import com.example.interfaces.DataServiceInterface;
import com.example.models.BillDataModel;
import com.example.models.JsonModel;

public class DataService extends Service_Base implements DataServiceInterface {
    /* 逻辑处理类 */
    private final DataControl dataControl;

    public DataService(String requestJsonStr) {
        super(requestJsonStr);
        dataControl = new DataControl();
    }

    /* 执行 */
    @Override
    public String run() {
        return switch (action) {
            case "append" -> appendBill();
            case "get" -> getBills();
            case "delete" -> deleteBill();
            default -> null;
        };
    }

    /* 创建(添加)账单 */
    @Override
    public String appendBill() {
        String username = (String) requestData.get("username");
        String personalIdentificationCode = (String) requestData.get("pic");
        BillDataModel bill = new BillDataModel((JsonModel) requestData.get("bill"));

        JsonModel responseJson = dataControl.appendBillData(username, personalIdentificationCode, bill);
        return responseJson.toString();
    }

    /* 获取账单 */
    @Override
    public String getBills() {
        String username = (String) requestData.get("username");
        String personalIdentificationCode = (String) requestData.get("pic");
        int count = (int) requestData.get("count");

        JsonModel responseJson = dataControl.getBillsData(username, personalIdentificationCode, count);
        return responseJson.toString();
    }

    /* 删除账单 */
    @Override
    public String deleteBill() {
        String username = (String) requestData.get("username");
        String personalIdentificationCode = (String) requestData.get("pic");
        int bid = (int) requestData.get("bid");

        JsonModel responseJson = dataControl.deleteBillData(username, personalIdentificationCode, bid);
        return responseJson.toString();
    }

    /* 更新账单 */
    @Override
    public String updateBill() {
        String username = (String) requestData.get("username");
        String personalIdentificationCode = (String) requestData.get("pic");
        BillDataModel bill = new BillDataModel((JsonModel) requestData.get("bill"));

        JsonModel responseJson = dataControl.updateBillData(username, personalIdentificationCode, bill);
        return responseJson.toString();
    }

    @Override
    public void close() {
        this.dataControl.close();
    }
}
